package controllers

import (
	"encoding/base64"
	"errmsg"
	"errors"
	"github.com/astaxie/beego/logs"
	"smart_home/api/models"
	"tools"
)

type EquipmentController struct {
	BaseController
}

// @Title Create
// @Description 注册设备
// @router /register [post]
func (c *EquipmentController) Register() {
	var (
		equipment       *models.Equipment
		err             error
		passDecode, pwd []byte
	)

	equipment = &models.Equipment{
		UserId: c.User.Id,
		Type:   1,
	}

	// 接收数据
	if err = c.ParseData(equipment); err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseParameter)
		return
	}

	// 验证数据
	if err = equipment.Validate(); err != nil {
		c.Error(err)
		return
	}

	// base64解码
	passDecode, err = base64.StdEncoding.DecodeString(equipment.Password)
	if err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}

	// rsa解密
	pwd, err = tools.Decrypt(passDecode)
	if err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	equipment.Password = string(pwd)

	// 初始化设备
	models.InitEquipment(equipment.Sta)

	if err = models.AddEquipment(equipment); err != nil {
		logs.Info(err)
		c.Error(errors.New("设备注册失败"))
		return
	}

	// 添加必要数据

	c.Success(nil)
}

// @Title GetAll
// @Description 获取设备列表
// @param scene_id path false "筛选场景"
// @param type     path false "设备类型"
// @router / [get]
func (c *EquipmentController) Lists() {
	var (
		page   int
		err    error
		sid    string
		ty     int
		params = make(map[string]interface{})
	)

	c.Ctx.Input.Bind(&sid, "scene_id")
	c.Ctx.Input.Bind(&page, "page")
	c.Ctx.Input.Bind(&ty, "type")
	if page == 0 {
		page = 1
	}

	if len(sid) > 0 {
		params["scene_id"] = sid
	}
	if ty > 0 {
		params["type"] = ty
	}

	lists, err := models.GetAllEquipmentByUid(c.User.Id, page, params)
	if err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseSystem)
	}

	c.Success(lists)
}

// @Title Get
// @Description 获取设备信息
// @Param	sta		path 	string	true		"equipment sta"
// @router /:sta [get]
func (c *EquipmentController) Get() {
	sta := c.GetString(":sta")
	e, err := models.GetEquipmentBySta(sta, c.User.Id)
	if err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if e == nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseNotFound)
		return
	}
	c.Success(e)
}

// @Title Update
// @Description 修改设备
// @router /:id [put]
func (c *EquipmentController) Put() {
	var (
		id        string
		equipment *models.Equipment
		err       error
	)
	id = c.GetString(":id", "")

	if equipment, err = models.GetEquipmentById(id); err != nil || equipment == nil {
		if err != nil {
			logs.Error(err)
		}
		c.Error(errmsg.ErrResponseSystem)
		return
	}

	//
	if equipment.UserId != c.User.Id {
		c.Error(errmsg.ErrResponseAuth)
		return
	}

	err = c.ParseData(equipment)
	if err != nil {
		logs.Info(err)
		c.Error(err)
		return
	}

	// 验证数据
	if err = equipment.Validate(); err != nil {
		c.Error(err)
		return
	}

	if !models.UpdateEquipment(equipment) {
		c.Error(errmsg.ErrResponseFail)
		return
	}

	c.Success(nil)
}

// @Title Delete
// @Description 删除设备
// @Param	id		path 	string	true		"sta"
// @router /:id [delete]
func (c *EquipmentController) Delete() {
	var (
		id  string
		err error
		eq  *models.Equipment
	)
	id = c.GetString(":id")

	if eq, err = models.GetEquipmentById(id); err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if eq == nil {
		err = errors.New("设备不存在")
		logs.Info(err)
		c.Error(err)
		return
	}

	if eq.UserId == c.User.Id { // 删除设备
		if err = models.InitEquipment(eq.Sta); err != nil {
			logs.Info(err)
			c.Error(errmsg.ErrResponseFail)
			return
		}
	} else { // 移除分享
		if err = models.RelieveFamilyBySta(eq.Sta, c.User.Id); err != nil {
			logs.Info(err)
			c.Error(errmsg.ErrResponseFail)
			return
		}
	}

	c.Success(nil)
}

// @Title Get
// @Description 设备操作日志
// @Param	sta		path 	string	true		"equipment sta"
// @router /log/:sta [get]
func (c *EquipmentController) Log() {
	var (
		sta   string
		page  int
		err   error
		lists []*models.EquipmentLog
	)

	sta = c.GetString(":sta")
	page, err = c.GetInt("page", 1)
	if err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseParameter)
		return
	}

	if lists, err = models.GetAllEquipmentLogBySta(sta, page); err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}

	c.Success(lists)
}

// @Title Get
// @Description 清除设备操作日志
// @Param	sta		path 	string	true		"equipment sta"
// @router /clear_log/:sta [delete]
func (c *EquipmentController) ClearLog() {
	sta := c.GetString(":sta")
	if err := models.ClearEquipmentLogBySta(sta); err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseFail)
		return
	}
	c.Success(nil)
}

// @Title Get
// @Description 设备最新版本详情
// @Param	sta		path 	string	true		"equipment sta"
// @router /version/info/:sta [get]
func (c *EquipmentController) VersionInfo() {
	c.Success(nil)
}
